<?php
/**
 * 生成配送地址js文件
 */

class pa_region{
	public function execute(){
		global $G_PGLOB;
		if(isset($_REQUEST['operFlg'])){
			$operFlg = $_REQUEST['operFlg'];
			switch($operFlg){
				case 1:
					$this->init();	//初始化页面
				break;
				case 2:
					$this->generateFile(); 	//生成js文件
				break;
				case 3:
					$this->siteFile(); //生成站点js文件
				break;
				default:
					$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
				break;		
			}
		}
	}
	
	/** 
	 * 初始化页面
	 *
	 */
	private function init(){
		global $G_PGLOB;
		
		$G_PGLOB->smarty->display('project/region.tpl');
	}
	
	/**
	 * 生成配送地址js文件
	 */
	private function generateFile(){
		global $G_PGLOB; 
		
		$sql = 'SELECT id,name FROM sys_region WHERE parentID = ?';
		$sqlResult = $G_PGLOB->DBCA->stmt_query($sql,'i',0);
		
		if($sqlResult){ //查到的是一级
			$regionArr = array();
			foreach($sqlResult as $rel){
				$sqltwo = 'SELECT id,name FROM sys_region WHERE parentID = ?';	
				$sqlTwoRel = $G_PGLOB->DBCA->stmt_query($sqltwo,'i',$rel->id); //二级 37,38,39
				if($sqlTwoRel){
					$twoArr = array();
					foreach($sqlTwoRel as $twoRel){
						$sqlThree = 'SELECT id,name,postCode FROM sys_region WHERE parentID = ?';
						$sqlThreeRel = $G_PGLOB->DBCA->stmt_query($sqlThree,'i',$twoRel->id);
						if($sqlThreeRel){
							$Arr = array();
							foreach($sqlThreeRel as $relt){
								$fourArr = array();
								$sqlFour = 'SELECT id,name FROM sys_region WHERE parentID = ?';
								$sqlFourRel = $G_PGLOB->DBCA->stmt_query($sqlFour,'i',$relt->id);
								if($sqlFourRel){
									foreach($sqlFourRel as $four_l){
										$fourArr[$four_l->id] = urlencode($four_l->name);
									}
									$Arr[$relt->id] = array(
										'n' => urlencode($relt->name),
										'p' => $relt->postCode,
										'c' => $fourArr,
									);
								}else{
									$Arr[$relt->id] = array(
										'n' => urlencode($relt->name),
										'p' => $relt->postCode,
									);
								}
							}
							
							$twoArr[$twoRel->id] = array(
								'n' => urlencode($twoRel->name),
								'a' => $twoRel->areaCode,
								'c' => $Arr,
							);
						}else{
							$twoArr[$twoRel->id] = array(
								'n' => urlencode($twoRel->name),
								'a' => $twoRel->areaCode,
							);
						}
					}

					$regionArr[$rel->id] = array(
						'n'=>urlencode($rel->name),
						'c'=>$twoArr,
					);
				}else{
					$regionArr[$rel->id] = array(
						'n'=>urlencode($rel->name),
					);
				}	
			}
		}

		$json = json_encode($regionArr);
		$region_js = 'var region_obj = ' . urldecode($json) . ';';
		
		//写入到文件
 		@mkdir("./shop/theme/xth2/js/",0777,true);
 		$writeInfo = file_put_contents("./shop/theme/xth2/js/region.js",$region_js);
		
 		$file_name = "region.js";
		header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
		header('Expires:0');
		header('Pragma:public');
		header("Content-type: text/inc");
		header("Content-Disposition: attachment; filename=" . $file_name);
		header("Content-Length: " . strlen($region_js));
		echo $region_js;
		
 		if (!file_put_contents(G_APPLICATION_BASEPATH.'shop/theme/xth2/js/region.js', $region_js)){
 			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
 			return;
 		}
		
		$G_PGLOB->smarty->display('project/region.tpl');
		echo "<script>comm_alert('更新成功',3);</script>";
	}
	/**
	 * 生成站点js文件
	 *
	 */
	private function siteFile () {
		global $G_PGLOB;
		
		$sql = 'SELECT site_id,siteName,alias FROM s_site WHERE useFlg = 1';
		$sqlResult = &$G_PGLOB->DBCA->stmt_query($sql);
		if ($sqlResult) {
			$siteArr = array();
			foreach ($sqlResult as $site) {
				$siteArr[$site->site_id] = array(
							'siteName' => urlencode($site->siteName),
							'alias'	=> $site->alias
						);
			}
		}
		
		$json = json_encode($siteArr);
		$region_js = 'var site_obj = ' . urldecode($json) . ';';
		
		if (!file_put_contents(G_APPLICATION_BASEPATH.'quanliadmin/docroot/js/site.js', $region_js)){
			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
			return;
		}
		if (!file_put_contents(G_APPLICATION_BASEPATH.'shop/lecake/theme/xth2/js/site.js', $region_js)){
			$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
			return;
		}
		
		$G_PGLOB->smarty->display('project/region.tpl');
		echo "<script>comm_alert('更新成功',3);</script>";
	}
}
?>